<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>3.4 接口与调用顺序 | 引言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../c03/s05.html" />
    
    
    <link rel="prev" href="../c03/s03.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.4"
        data-chapter-title="3.4 接口与调用顺序"
        data-filepath="c03/s04.md"
        data-basepath=".."
        data-revision="Fri Feb 10 2017 17:36:30 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        引言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="c01/index.html">
            
                
                    <a href="../c01/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        1 关于Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="c01/s01.html">
            
                
                    <a href="../c01/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        1.1 Tornado是为何物
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="c01/s02.html">
            
                
                    <a href="../c01/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        1.2 Tornado与Django
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="c02/index.html">
            
                
                    <a href="../c02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        2 初识Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="c02/s01.html">
            
                
                    <a href="../c02/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        2.1 安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="c02/s02.html">
            
                
                    <a href="../c02/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        2.2 Hello Itcast
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="c02/s03.html">
            
                
                    <a href="../c02/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        2.3 httpserver
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="c02/s04.html">
            
                
                    <a href="../c02/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        2.4 options
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="c02/s05.html">
            
                
                    <a href="../c02/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        2.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="c03/index.html">
            
                
                    <a href="../c03/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        3 深入Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="c03/s01.html">
            
                
                    <a href="../c03/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        3.1 Application
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="c03/s02.html">
            
                
                    <a href="../c03/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        3.2 输入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="c03/s03.html">
            
                
                    <a href="../c03/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        3.3 输出
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.4" data-path="c03/s04.html">
            
                
                    <a href="../c03/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        3.4 接口与调用顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="c03/s05.html">
            
                
                    <a href="../c03/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        3.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="c04/index.html">
            
                
                    <a href="../c04/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        4 模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="c04/s01.html">
            
                
                    <a href="../c04/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        4.1 静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="c04/s02.html">
            
                
                    <a href="../c04/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        4.2 使用模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="c04/s03.html">
            
                
                    <a href="../c04/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        4.3 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="c05/index.html">
            
                
                    <a href="../c05/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        5 数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="c05/s01.html">
            
                
                    <a href="../c05/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        5.1 数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="c05/s02.html">
            
                
                    <a href="../c05/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        5.2 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="c06/index.html">
            
                
                    <a href="../c06/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        6 安全应用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="c06/s01.html">
            
                
                    <a href="../c06/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        6.1 Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="c06/s02.html">
            
                
                    <a href="../c06/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        6.2 XSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="c06/s03.html">
            
                
                    <a href="../c06/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        6.3 用户验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="c06/s04.html">
            
                
                    <a href="../c06/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        6.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="c07/index.html">
            
                
                    <a href="../c07/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        7 异步与WebSocket
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="c07/s01.html">
            
                
                    <a href="../c07/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        7.1 认识异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="c07/s02.html">
            
                
                    <a href="../c07/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        7.2 Tornado异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="c07/s03.html">
            
                
                    <a href="../c07/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        7.3 WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="c07/s04.html">
            
                
                    <a href="../c07/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        7.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="c08/index.html">
            
                
                    <a href="../c08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        8 部署
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="c08/s01.html">
            
                
                    <a href="../c08/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        8.1 部署Tornado
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >引言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="34-&#x63A5;&#x53E3;&#x4E0E;&#x8C03;&#x7528;&#x987A;&#x5E8F;">3.4 &#x63A5;&#x53E3;&#x4E0E;&#x8C03;&#x7528;&#x987A;&#x5E8F;</h1>
<p>&#x4E0B;&#x9762;&#x7684;&#x63A5;&#x53E3;&#x65B9;&#x6CD5;&#x662F;&#x7531;tornado&#x6846;&#x67B6;&#x8FDB;&#x884C;&#x8C03;&#x7528;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x6027;&#x7684;&#x91CD;&#x5199;&#x8FD9;&#x4E9B;&#x65B9;&#x6CD5;&#x3002;</p>
<h2 id="1-initialize">1. initialize()</h2>
<p>&#x5BF9;&#x5E94;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x7684;&#x5904;&#x7406;&#x7C7B;Handler&#x5728;&#x6784;&#x9020;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x540E;&#x9996;&#x5148;&#x6267;&#x884C;initialize()&#x65B9;&#x6CD5;&#x3002;&#x5728;&#x8BB2;&#x8F93;&#x5165;&#x65F6;&#x63D0;&#x5230;&#xFF0C;<strong>&#x8DEF;&#x7531;&#x6620;&#x5C04;&#x4E2D;&#x7684;&#x7B2C;&#x4E09;&#x4E2A;&#x5B57;&#x5178;&#x578B;&#x53C2;&#x6570;&#x4F1A;&#x4F5C;&#x4E3A;&#x8BE5;&#x65B9;&#x6CD5;&#x7684;&#x547D;&#x540D;&#x53C2;&#x6570;&#x4F20;&#x9012;&#xFF0C;</strong>&#x5982;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ProfileHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">initialize</span><span class="hljs-params">(self, database)</span>:</span>
        self.database = database

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        ...

app = Application([
    (<span class="hljs-string">r&apos;/user/(.*)&apos;</span>, ProfileHandler, dict(database=database)),
    ])
</code></pre>
<p><strong>&#x6B64;&#x65B9;&#x6CD5;&#x901A;&#x5E38;&#x7528;&#x6765;&#x521D;&#x59CB;&#x5316;&#x53C2;&#x6570;&#xFF08;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#xFF09;&#xFF0C;&#x5F88;&#x5C11;&#x4F7F;&#x7528;&#x3002;</strong></p>
<h2 id="2-prepare">2. prepare()</h2>
<p>&#x9884;&#x5904;&#x7406;&#xFF0C;&#x5373;&#x5728;&#x6267;&#x884C;&#x5BF9;&#x5E94;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x7684;HTTP&#x65B9;&#x6CD5;&#xFF08;&#x5982;get&#x3001;post&#x7B49;&#xFF09;&#x524D;&#x5148;&#x6267;&#x884C;&#xFF0C;<strong>&#x6CE8;&#x610F;&#xFF1A;&#x4E0D;&#x8BBA;&#x4EE5;&#x4F55;&#x79CD;HTTP&#x65B9;&#x5F0F;&#x8BF7;&#x6C42;&#xFF0C;&#x90FD;&#x4F1A;&#x6267;&#x884C;prepare()&#x65B9;&#x6CD5;</strong>&#x3002;</p>
<p>&#x4EE5;&#x9884;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x4F53;&#x4E2D;&#x7684;json&#x6570;&#x636E;&#x4E3A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">prepare</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">if</span> self.request.headers.get(<span class="hljs-string">&quot;Content-Type&quot;</span>).startswith(<span class="hljs-string">&quot;application/json&quot;</span>):
            self.json_dict = json.loads(self.request.body)
        <span class="hljs-keyword">else</span>:
            self.json_dict = <span class="hljs-keyword">None</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">if</span> self.json_dict:
            <span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> self.json_dict.items():
                self.write(<span class="hljs-string">&quot;&lt;h3&gt;%s&lt;/h3&gt;&lt;p&gt;%s&lt;/p&gt;&quot;</span> % (key, value))

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">put</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">if</span> self.json_dict:
            <span class="hljs-keyword">for</span> key, value <span class="hljs-keyword">in</span> self.json_dict.items():
                self.write(<span class="hljs-string">&quot;&lt;h3&gt;%s&lt;/h3&gt;&lt;p&gt;%s&lt;/p&gt;&quot;</span> % (key, value))
</code></pre>
<p>&#x7528;post&#x65B9;&#x5F0F;&#x53D1;&#x9001;json&#x6570;&#x636E;&#x65F6;&#xFF1A;</p>
<p><img src="../images/prepare_post.png" alt="post&#x65B9;&#x5F0F;"></p>
<p>&#x7528;put&#x65B9;&#x5F0F;&#x53D1;&#x9001;json&#x6570;&#x636E;&#x65F6;&#xFF1A;</p>
<p><img src="../images/prepare_put.png" alt="put&#x65B9;&#x5F0F;"></p>
<h2 id="3-http&#x65B9;&#x6CD5;">3. HTTP&#x65B9;&#x6CD5;</h2>
<table>
<thead>
<tr>
<th style="text-align:left">&#x65B9;&#x6CD5;</th>
<th style="text-align:left">&#x63CF;&#x8FF0; </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">get</td>
<td style="text-align:left">&#x8BF7;&#x6C42;&#x6307;&#x5B9A;&#x7684;&#x9875;&#x9762;&#x4FE1;&#x606F;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x5B9E;&#x4F53;&#x4E3B;&#x4F53;&#x3002;</td>
</tr>
<tr>
<td style="text-align:left">head</td>
<td style="text-align:left">&#x7C7B;&#x4F3C;&#x4E8E;get&#x8BF7;&#x6C42;&#xFF0C;&#x53EA;&#x4E0D;&#x8FC7;&#x8FD4;&#x56DE;&#x7684;&#x54CD;&#x5E94;&#x4E2D;&#x6CA1;&#x6709;&#x5177;&#x4F53;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x7528;&#x4E8E;&#x83B7;&#x53D6;&#x62A5;&#x5934;</td>
</tr>
<tr>
<td style="text-align:left">post</td>
<td style="text-align:left">&#x5411;&#x6307;&#x5B9A;&#x8D44;&#x6E90;&#x63D0;&#x4EA4;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#xFF08;&#x4F8B;&#x5982;&#x63D0;&#x4EA4;&#x8868;&#x5355;&#x6216;&#x8005;&#x4E0A;&#x4F20;&#x6587;&#x4EF6;&#xFF09;&#x3002;&#x6570;&#x636E;&#x88AB;&#x5305;&#x542B;&#x5728;&#x8BF7;&#x6C42;&#x4F53;&#x4E2D;&#x3002;POST&#x8BF7;&#x6C42;&#x53EF;&#x80FD;&#x4F1A;&#x5BFC;&#x81F4;&#x65B0;&#x7684;&#x8D44;&#x6E90;&#x7684;&#x5EFA;&#x7ACB;&#x548C;/&#x6216;&#x5DF2;&#x6709;&#x8D44;&#x6E90;&#x7684;&#x4FEE;&#x6539;&#x3002;</td>
</tr>
<tr>
<td style="text-align:left">delete</td>
<td style="text-align:left">&#x8BF7;&#x6C42;&#x670D;&#x52A1;&#x5668;&#x5220;&#x9664;&#x6307;&#x5B9A;&#x7684;&#x5185;&#x5BB9;&#x3002;</td>
</tr>
<tr>
<td style="text-align:left">patch</td>
<td style="text-align:left">&#x8BF7;&#x6C42;&#x4FEE;&#x6539;&#x5C40;&#x90E8;&#x6570;&#x636E;&#x3002;</td>
</tr>
<tr>
<td style="text-align:left">put</td>
<td style="text-align:left">&#x4ECE;&#x5BA2;&#x6237;&#x7AEF;&#x5411;&#x670D;&#x52A1;&#x5668;&#x4F20;&#x9001;&#x7684;&#x6570;&#x636E;&#x53D6;&#x4EE3;&#x6307;&#x5B9A;&#x7684;&#x6587;&#x6863;&#x7684;&#x5185;&#x5BB9;&#x3002;</td>
</tr>
<tr>
<td style="text-align:left">options</td>
<td style="text-align:left">&#x8FD4;&#x56DE;&#x7ED9;&#x5B9A;URL&#x652F;&#x6301;&#x7684;&#x6240;&#x6709;HTTP&#x65B9;&#x6CD5;&#x3002;</td>
</tr>
</tbody>
</table>
<h2 id="4-onfinish">4. on_finish()</h2>
<p>&#x5728;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x7ED3;&#x675F;&#x540E;&#x8C03;&#x7528;&#xFF0C;&#x5373;&#x5728;&#x8C03;&#x7528;HTTP&#x65B9;&#x6CD5;&#x540E;&#x8C03;&#x7528;&#x3002;&#x901A;&#x5E38;&#x8BE5;&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x8FDB;&#x884C;&#x8D44;&#x6E90;&#x6E05;&#x7406;&#x91CA;&#x653E;&#x6216;&#x5904;&#x7406;&#x65E5;&#x5FD7;&#x7B49;&#x3002;<strong>&#x6CE8;&#x610F;&#xFF1A;&#x8BF7;&#x5C3D;&#x91CF;&#x4E0D;&#x8981;&#x5728;&#x6B64;&#x65B9;&#x6CD5;&#x4E2D;&#x8FDB;&#x884C;&#x54CD;&#x5E94;&#x8F93;&#x51FA;&#x3002;</strong></p>
<h2 id="5-setdefaultheaders">5. set_default_headers()</h2>
<h2 id="6-writeerror">6. write_error()</h2>
<h2 id="7-&#x8C03;&#x7528;&#x987A;&#x5E8F;">7. &#x8C03;&#x7528;&#x987A;&#x5E8F;</h2>
<p>&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4E00;&#x6BB5;&#x7A0B;&#x5E8F;&#x6765;&#x770B;&#x4E0A;&#x9762;&#x8FD9;&#x4E9B;&#x63A5;&#x53E3;&#x7684;&#x8C03;&#x7528;&#x987A;&#x5E8F;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">initialize</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x8C03;&#x7528;&#x4E86;initialize()&quot;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">prepare</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x8C03;&#x7528;&#x4E86;prepare()&quot;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_default_headers</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x8C03;&#x7528;&#x4E86;set_default_headers()&quot;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_error</span><span class="hljs-params">(self, status_code, **kwargs)</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x8C03;&#x7528;&#x4E86;write_error()&quot;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x8C03;&#x7528;&#x4E86;get()&quot;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x8C03;&#x7528;&#x4E86;post()&quot;</span>
        self.send_error(<span class="hljs-number">200</span>)  <span class="hljs-comment"># &#x6CE8;&#x610F;&#x6B64;&#x51FA;&#x629B;&#x51FA;&#x4E86;&#x9519;&#x8BEF;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_finish</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">print</span> <span class="hljs-string">&quot;&#x8C03;&#x7528;&#x4E86;on_finish()&quot;</span>
</code></pre>
<p><img src="../images/interface_sequence.png" alt="&#x8C03;&#x7528;&#x987A;&#x5E8F;"></p>
<p><strong>&#x5728;&#x6B63;&#x5E38;&#x60C5;&#x51B5;&#x672A;&#x629B;&#x51FA;&#x9519;&#x8BEF;&#x65F6;&#xFF0C;&#x8C03;&#x7528;&#x987A;&#x5E8F;&#x4E3A;&#xFF1A;</strong></p>
<ol>
<li>set_defautl_headers()</li>
<li>initialize()</li>
<li>prepare()</li>
<li>HTTP&#x65B9;&#x6CD5;</li>
<li>on_finish()</li>
</ol>
<p><strong>&#x5728;&#x6709;&#x9519;&#x8BEF;&#x629B;&#x51FA;&#x65F6;&#xFF0C;&#x8C03;&#x7528;&#x987A;&#x5E8F;&#x4E3A;&#xFF1A;</strong></p>
<ol>
<li>set_default_headers()</li>
<li>initialize()</li>
<li>prepare()</li>
<li>HTTP&#x65B9;&#x6CD5;</li>
<li><strong>set_default_headers()</strong></li>
<li>write_error()</li>
<li>on_finish()</li>
</ol>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../c03/s03.html" class="navigation navigation-prev " aria-label="Previous page: 3.3 输出"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../c03/s05.html" class="navigation navigation-next " aria-label="Next page: 3.5 练习"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
